import {ApiClient} from './api'
import {createMessageConfig} from '@/utils/responseHandler'

// 用户相关类型定义
export interface User {
  id: number
  username: string
  displayName?: string
  email?: string
  userType: string
  status: string
  lastLoginTime?: string
  createTime: string
  updateTime: string
}

// 用户管理API
export const userService = {
  /**
   * 获取所有用户
   */
  getUsers: () => {
    return ApiClient.get<User[]>('/users/all', { showSuccess: false })
  },

  /**
   * 根据ID获取用户
   */
  getUserById: (id: number) => {
    return ApiClient.get<User>(`/users/${id}`, { showSuccess: false })
  },

  /**
   * 创建用户
   */
  createUser: (data: Partial<User>) => {
    return ApiClient.post<User>('/users', data, createMessageConfig('用户创建成功'))
  },

  /**
   * 更新用户
   */
  updateUser: (id: number, data: Partial<User>) => {
    return ApiClient.put<User>(`/users/${id}`, data, createMessageConfig('用户更新成功'))
  },

  /**
   * 删除用户
   */
  deleteUser: (id: number) => {
    return ApiClient.delete<void>(`/users/${id}`, createMessageConfig('用户删除成功'))
  },

  /**
   * 重置用户密码
   */
  resetPassword: (id: number) => {
    return ApiClient.post<string>(`/users/${id}/reset-password`, {}, createMessageConfig('密码重置成功，请查看临时密码'))
  },

  /**
   * 启用/禁用用户
   */
  updateUserStatus: (id: number, status: string) => {
    const message = status === 'ACTIVE' ? '用户启用成功' : '用户禁用成功'
    return ApiClient.patch<User>(`/users/${id}/status`, { status }, createMessageConfig(message))
  },

  /**
   * 切换用户状态
   */
  toggleUserStatus: (id: number) => {
    return ApiClient.post<User>(`/users/${id}/toggle-status`, {}, createMessageConfig('用户状态切换成功'))
  },

  /**
   * 锁定用户
   */
  lockUser: (id: number) => {
    return ApiClient.post<User>(`/users/${id}/lock`, {}, createMessageConfig('用户锁定成功'))
  },

  /**
   * 解锁用户
   */
  unlockUser: (id: number) => {
    return ApiClient.post<User>(`/users/${id}/unlock`, {}, createMessageConfig('用户解锁成功'))
  }
}

// 导出具体方法以便按需导入
export const {
  getUsers,
  getUserById,
  createUser,
  updateUser,
  deleteUser,
  resetPassword,
  updateUserStatus
} = userService